package com.song.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.song.annotation.AutoFill;
import com.song.context.BaseContext;
import com.song.dto.SetmealDTO;
import com.song.dto.SetmealPageQueryDTO;
import com.song.entity.Setmeal;
import com.song.entity.SetmealDish;
import com.song.enumeration.SqlOperationType;
import com.song.mapper.SetmealMapper;
import com.song.result.PageResult;
import com.song.service.SetmealService;
import com.song.vo.DishItemVO;
import com.song.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import java.util.List;

@Service
public class setmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public List<Setmeal> getSetmealList(Setmeal setmeal) {
       return setmealMapper.getSetmealList(setmeal);
    }

    @Override
    public List<DishItemVO> getDishItemBySetmealId(Long id) {
       return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public void updateSetmeal(SetmealVO setmealVO) {
        //1.更新套餐表setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        //2.更新套餐菜品关系表setmeal_dish
        //2.1根据套餐id删除套餐菜品关系表中的数据
        setmealMapper.deleteSetmealDishBySetmealId(setmealVO.getId());
        //2.2插入新的套餐菜品关系数据
        List<SetmealDish> setmealDishs = setmealVO.getSetmealDishes();
        setmealMapper.insertSetmealDish(setmealDishs);
    }

    @Override
    public PageResult<SetmealVO> getSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO,setmeal);
        List<SetmealVO> setmealVOList = setmealMapper.getSetmealPage(setmeal);
        Page page =(Page) setmealVOList;
        return new PageResult<>(page.getTotal(), setmealVOList);

    }

    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        setmealMapper.updateSetmealStatus(status,id);
    }

    @Override
    public void deleteSetmeals(List<Long> ids) {
        //删除套餐表中的数据
        setmealMapper.deleteSetmealByIds(ids);
        //删除套餐菜品关系表数据
        ids.forEach(id -> setmealMapper.deleteSetmealDishBySetmealId(id));
    }
    @AutoFill(SqlOperationType.INSERT)
    @Transactional
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //插入套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.insertSetmeal(setmeal);
        //插入套餐-菜品关系表数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealMapper.insertSetmealDish(setmealDishes);

    }

    @Override
    public SetmealVO getSetmealById(Long id) {
       // 查询套餐数据
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        SetmealVO setmealVO = (SetmealVO) setmealMapper.getSetmealPage(setmeal);
       // 查询套餐菜品数据
       setmealVO.setSetmealDishes(setmealMapper.getSetmealAnaDish(id));
       return setmealVO;
    }
}
